package com.example.activitysys.controller;

import com.example.activitysys.mapper.LostItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计控制器
 */
@Controller
@RequestMapping("/stats")
public class StatsController {

    @Autowired
    private LostItemMapper lostItemMapper;

    /**
     * 统计页面
     */
    @GetMapping("/dashboard")
    public String dashboard(Model model, HttpSession session) {
        // 检查登录状态
        if (session.getAttribute("admin") == null) {
            return "redirect:/login";
        }
        
        return "stats/dashboard";
    }

    /**
     * 按地点统计失物数量（API接口）
     */
    @GetMapping("/api/lost-by-location")
    @ResponseBody
    public List<Map<String, Object>> getLostByLocation(HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return null;
        }
        return lostItemMapper.selectLostCountByLocation();
    }

    /**
     * 按分类统计失物数量（API接口）
     */
    @GetMapping("/api/lost-by-category")
    @ResponseBody
    public List<Map<String, Object>> getLostByCategory(HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return null;
        }
        return lostItemMapper.selectLostCountByCategory();
    }



    /**
     * 总体统计数据（API接口）
     */
    @GetMapping("/api/overview")
    @ResponseBody
    public Map<String, Object> getOverview(HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return null;
        }
        
        Map<String, Object> result = new HashMap<>();
        
        // 失物统计
        List<Map<String, Object>> lostByCategory = lostItemMapper.selectLostCountByCategory();
        int totalLost = lostByCategory.stream().mapToInt(item -> ((Number) item.get("count")).intValue()).sum();
        
        result.put("totalLost", totalLost);
        result.put("lostByCategory", lostByCategory);
        
        return result;
    }

    /**
     * 已认领数量统计（API接口）
     */
    @GetMapping("/api/claimed-count")
    @ResponseBody
    public Map<String, Object> getClaimedCount(HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return null;
        }
        
        Map<String, Object> result = new HashMap<>();
        
        // 查询已认领的失物数量
        Long claimedCount = lostItemMapper.selectCount(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.example.activitysys.entity.LostItem>()
                .eq("status", 1)
                .eq("deleted", 0)
        );
        
        result.put("claimedCount", claimedCount);
        
        return result;
    }
}